package skin.arvin.com.testlocation;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

        LocationManager mLocationManager;
        Location mlocation;
        TextView mTextView;
        TextView mTextView2;
        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mTextView = (TextView) findViewById(R.id.textView1);
            mTextView2 = (TextView) findViewById(R.id.textView2);


        }

        @SuppressLint("MissingPermission")
        public Location getLocation() {
            mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
                mLocationManager.addNmeaListener(mNmeaListener);
            }

            return mlocation;
        }


    @Override
    protected void onResume() {
        super.onResume();
        getLocation();
    }

    //这里获取的数据就是在之前一个activity写进去的数据
        LocationListener mLocationListener = new LocationListener() {
            //这个数据是经过LocationManager
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
            @Override
            public void onLocationChanged(Location mlocal) {
                if (mlocal == null) return;
                String strResult = "getAccuracy:" + mlocal.getAccuracy() + "\r\n"
                        + "getAltitude:" + mlocal.getAltitude() + "\r\n"
                        + "getBearing:" + mlocal.getBearing() + "\r\n"
                        + "getElapsedRealtimeNanos:" + String.valueOf(mlocal.getElapsedRealtimeNanos()) + "\r\n"
                        + "getLatitude:" + mlocal.getLatitude() + "\r\n"
                        + "getLongitude:" + mlocal.getLongitude() + "\r\n"
                        + "getProvider:" + mlocal.getProvider() + "\r\n"
                        + "getSpeed:" + mlocal.getSpeed() + "\r\n"
                        + "getTime:" + mlocal.getTime() + "\r\n";
                Log.i("Show", strResult);
                if (mTextView != null) {
                    mTextView.setText(strResult);
                }

                onStatusChanged("gps", 100, mlocal.getExtras());
            }

            @Override
            public void onProviderDisabled(String arg0) {
            }

            @Override
            public void onProviderEnabled(String arg0) {
            }

            @Override
            public void onStatusChanged(String provider, int event, Bundle extras) {
                if (event ==100){
                    String strResult = extras.getString("test1","") +"\n" +
                            extras.getString("test2","");

                    if (mTextView2 != null) {
                        mTextView2.setText(strResult);
                    }
                }

            }
        };

        //原始数据监听
        GpsStatus.NmeaListener mNmeaListener = new GpsStatus.NmeaListener() {
            @Override
            public void onNmeaReceived(long arg0, String arg1) {
                byte[] bytes = arg1.getBytes();
            }
        };

        @Override
        protected void onDestroy() {
            super.onDestroy();
            if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                mLocationManager.removeNmeaListener(mNmeaListener);
                mLocationManager.removeUpdates(mLocationListener);
            }
        }

}
